package com.joyxy.spider.mapscript;

import java.util.List;
import java.util.ArrayList;

/**
 * 脚本层定义
 * 格式：Map%XMin%XMax%YMin%YMax%OP1+OP2+...
 */
public class ScriptLayer {
    /** 地图编号 */
    private int mapId;
    /** 该层最小横坐标 */
    private int xMin;
    /** 该层最大横坐标 */
    private int xMax;
    /** 该层最小纵坐标 */
    private int yMin;
    /** 该层最大纵坐标 */
    private int yMax;
    /** 该层所要执行的命令列表 */
    private List<ScriptOperation> operations;
    
    public ScriptLayer() {
        this.operations = new ArrayList<>();
    }
    
    public ScriptLayer(int mapId, int xMin, int xMax, int yMin, int yMax) {
        this.mapId = mapId;
        this.xMin = xMin;
        this.xMax = xMax;
        this.yMin = yMin;
        this.yMax = yMax;
        this.operations = new ArrayList<>();
    }
    
    // Getter and Setter methods
    public int getMapId() {
        return mapId;
    }
    
    public void setMapId(int mapId) {
        this.mapId = mapId;
    }
    
    public int getxMin() {
        return xMin;
    }
    
    public void setxMin(int xMin) {
        this.xMin = xMin;
    }
    
    public int getxMax() {
        return xMax;
    }
    
    public void setxMax(int xMax) {
        this.xMax = xMax;
    }
    
    public int getyMin() {
        return yMin;
    }
    
    public void setyMin(int yMin) {
        this.yMin = yMin;
    }
    
    public int getyMax() {
        return yMax;
    }
    
    public void setyMax(int yMax) {
        this.yMax = yMax;
    }
    
    public List<ScriptOperation> getOperations() {
        return operations;
    }
    
    public void setOperations(List<ScriptOperation> operations) {
        this.operations = operations;
    }
    
    public void addOperation(ScriptOperation operation) {
        this.operations.add(operation);
    }
    
    /**
     * 检查指定坐标是否在当前层范围内
     */
    public boolean isInRange(int x, int y) {
        return x >= xMin && x <= xMax && y >= yMin && y <= yMax;
    }
    
    /**
     * 执行该层的所有操作
     */
    public void execute() {
        System.out.println("开始执行层: " + this.toString());
        for (ScriptOperation operation : operations) {
            try {
                // 转换为具体操作对象并执行
                operation.toBaseOperation().execute();
            } catch (Exception e) {
                System.err.println("执行操作失败: " + operation + ", 错误: " + e.getMessage());
            }
        }
        System.out.println("层执行完成");
    }
    
    /**
     * 执行该层中指定坐标范围内的操作
     */
    public void executeForPosition(int x, int y) {
        if (!isInRange(x, y)) {
            System.out.println("坐标(" + x + "," + y + ")不在当前层范围内");
            return;
        }
        
        System.out.println("在坐标(" + x + "," + y + ")执行层操作");
        for (ScriptOperation operation : operations) {
            try {
                // 对于有区域限制的操作，检查是否在操作区域内
                if (operation.getParameterCount() >= 4) {
                    if (operation.isInOperationArea(x, y)) {
                        operation.toBaseOperation().execute();
                    }
                } else {
                    // 对于没有区域限制的操作，直接执行
                    operation.toBaseOperation().execute();
                }
            } catch (Exception e) {
                System.err.println("执行操作失败: " + operation + ", 错误: " + e.getMessage());
            }
        }
    }
    
    @Override
    public String toString() {
        return String.format("ScriptLayer{mapId=%d, xMin=%d, xMax=%d, yMin=%d, yMax=%d, operations=%d}", 
                mapId, xMin, xMax, yMin, yMax, operations.size());
    }
}
